#include "mainwindow.h"
#include "ui_mainwindow.h"

// Qt核心模块
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QAction>

// Qt小部件模块
#include <QTextEdit>

// Qt对话框模块
#include <QFileDialog>

// 用于调试输出
#include <QDebug>

// C++标准库头文件
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 设置标题
    this->setWindowTitle("我的记事本");

    // 创建菜单栏
    QMenuBar* menuBar = new QMenuBar(this);
    this->setMenuBar(menuBar);

    // 创建菜单
    QMenu* menu = new QMenu("文件");
    menuBar->addMenu(menu);

    // 创建菜单项
    QAction* action1 = new QAction("保存");
    QAction* action2 = new QAction("加载");
    menu->addAction(action1);
    menu->addAction(action2);

    // 创建中央控件
    QTextEdit* edit = new QTextEdit(this);
    this->setCentralWidget(edit);
    edit->setPlaceholderText("此处编写⽂本内容...");

    // 连接信号槽,点击 action 时触发一定的效果
    connect(action1, &QAction::triggered, this, &MainWindow::save);
    connect(action2, &QAction::triggered, this, &MainWindow::load);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::save()
{
    // 弹出对话框,选择写入文件的路径
    QFileDialog* dialog = new QFileDialog(this);
    QString fileName = dialog->getSaveFileName(this, "保存⽂件", "C:/Users/1/");
    qDebug() << "fileName: " << fileName;

    // 写⼊⽂件
    std::ofstream file(fileName.toStdString().c_str());
    if (!file.is_open()) {
        qDebug() << "文件保存失败!";
        return;
    }
    const QString& text = edit->toPlainText();
    file << text.toStdString();
    file.close();
}

void MainWindow::load()
{
    // 弹出对话框,选择打开的文件
    QFileDialog* dialog = new QFileDialog(this);
    QString fileName = dialog->getOpenFileName(this, "加载文件", "C:/Users/1/");
    qDebug() << "fileName: " << fileName;

    // 读取文件
    std::ifstream file(fileName.toStdString().c_str());
    if (!file.is_open()) {
        qDebug() << "文件加载失败!";
        return;
    }
    std::string content;
    std::string line;
    while (std::getline(file, line)) {
        content += line;
        content += "\n";
    }
    file.close();

    // 显示到界面上
    QString text = QString::fromStdString(content);
    edit->setPlainText(text);
}
